module.exports = function ($scope, $state, $resource, $modal, toaster){
	
	//查询渠道列表
	$scope.search = function (pageNo){
		var para = {
			'pageNo': pageNo || $scope.currentPage,
			'pageSize': $scope.itemsPerPage,
			'channel_list_name': $scope.searchform.channel_name
		}
		$resource('/api/as/tc/salechannel/findAllList', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				$scope.list = res.data.results;
				$scope.totalItems = res.data.totalRecord;
			}else{
				toaster.error({title: '', body: res.errmsg});
			}
		});
	}

	//开启|停用
	$scope.choose = function(obj, info){
		var para = {
			'channel_code': obj.channel_list_code,
			'company_code': info.company_code
		}
		var url = obj.status=='1'?'/api/as/tc/salechannel/addCompanyChannel':
									'/api/as/tc/salechannel/deleteCompanyChannel';
		$resource(url, {}, {}).save(para, function(res){
			if(res.errcode === 0){
				toaster.success({title: '', body: '成功'});
				$scope.search($scope.currentPage);
			} else {
				toaster.error({title: '', body: '失败'});
				obj.status = obj.status=='1'?'0':'1';
			}
		});
	}

	init();
    //初始化列表
    function init(){
		$scope.currentPage = 1;		//当前页码
		$scope.itemsPerPage = 10;	//每页显示几条
		$scope.searchform = {};
		$scope.search(1);
	}

	//查看销售品
	$scope.getSaleType = function(info) {
		$state.go('app.channelSale', {'name': info.channel_list_name, 'code': info.channel_list_code});
	}

	//设置或取消默认渠道
	$scope.change = function(obj, info) {
		var para = {
			'is_default': obj.is_default,
			'company_code': info.company_code,
			'channel_code': obj.channel_list_code
		}
		$resource('/api/as/tc/salechannel/setDefaultChannel', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				toaster.success({title: '', body: '修改默认渠道成功'});
			}else{
				toaster.error({title: '', body: '修改默认渠道失败,请重试'});
			}
		});
	}

};